package com.jdk.demo.reflect.clazz;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class GenericReturnTypeDemo {

	public static void main(String[] args) throws Exception {
		Class<?> clazz = Class.forName("com.jdk.demo.reflect.User");
		Method method = clazz.getMethod("getAddress");
        Class<?> returnType = method.getReturnType();
        if (returnType == List.class){
            Type type = method.getGenericReturnType();
            if (type instanceof ParameterizedType){
                Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
                //因为list泛型只有一个值 所以直接取0下标
                String typeName = actualTypeArguments[0].getTypeName();
                System.out.println(typeName);
                //真实返回值类型 Class对象
                Class<?> actualType = Class.forName(typeName);
                System.out.println(actualType);
            }
        }
	}

}
